import { defineStore } from 'pinia'
import { reactive, toRefs } from 'vue'
import { menuApi } from '@/api/modules/system/menu'
import type { LoginMenu } from '@/typings'

export const homeMenuPath = 'workbench'
const homeMenus: LoginMenu[] = [
  {
    menuId: homeMenuPath,
    menuName: '工作桌面',
    path: homeMenuPath,
    component: 'workbench/index',
    parentId: '',
    iconClass: 'al-workbench',
    hidden: 0,
    funcKeys: [],
    children: [],
    affix: true, //初始添加到tags
    noCache: false
  }
]

export const useMenuStore = defineStore('menu', () => {
  const state = reactive({
    menus: [] as LoginMenu[],
    menuIndex: {} as { [key: string]: LoginMenu }
  })
  const findAuthMenu = async () => {
    await menuApi.getMyMenu().then(resp => {
      if (resp.success) {
        state.menus = homeMenus.concat(resp.data)
      }
    })
  }
  const setMenuIndex = (menu: LoginMenu) => {
    state.menuIndex[menu.menuId] = menu
  }

  return { ...toRefs(state), findAuthMenu, setMenuIndex }
})
